import modelExtend from 'dva-model-extend'
import { message } from 'antd'
import { routerRedux } from 'dva/router';
import {
    queryNotreat, queryIng, queryEnd,addRepair,allowRepair,rejectRepair,commentRepair
} from 'services/repairManage'
import { pageModel } from './common'
import queryString from 'query-string';

export default modelExtend(pageModel, {
    namespace: 'repairManage',

    state: {

    },

    subscriptions: {
        setup({ dispatch, history }) {
            history.listen((location) => {
                console.log(location)
                if (location.pathname.includes('/chzu/notreatList')){
                    dispatch({
                        type:'queryNotreat',
                        payload:{
                            size: 10,
                            start: 1,
                        }
                    })
                }else if(location.pathname.includes('/chzu/ingList')){
                    dispatch({
                        type:'queryIng',
                        payload:{
                            size: 10,
                            start: 1
                        }
                    })
                }else if(location.pathname.includes('/chzu/endList')){
                    dispatch({
                        type:'queryEnd',
                        payload:{
                            size: 10,
                            start: 1,
                            success: -1
                        }
                    })
                }
            })
        },
    },

    effects: {
        //待报修
        *queryNotreat({payload},{call,put}){
            const data = yield call(queryNotreat,payload);
            console.log(data)
            if(data.code === '200'){
                const notreatList = data.data.page;
                const allCount = data.data.allcount;
                yield put({
                    type:'updateState',
                    payload:notreatList
                });
                yield put({
                    type:'updateAll',
                    payload:allCount
                })
            }else{
                message.error(data.msg);
            }
        },
        //正在报修
        *queryIng({payload},{call,put}){
            const data = yield call(queryIng,payload);
            console.log(data)
            if(data.code === '200'){
                const ingList = data.data.page;
                const allCount = data.data.allcount;
                yield put({
                    type:'updateIng',
                    payload:ingList
                });
                yield put({
                    type:'updateAll',
                    payload:allCount
                })
            }else{
                message.error(data.msg)
            }
        },
        //完成报修列表
        *queryEnd({payload},{call,put}){
            const data = yield call(queryEnd,payload);
            console.log(data)
            if(data.code === '200'){
                const endList = data.data.page;
                const allCount = data.data.allcount;
                yield put({
                    type:'updateEnd',
                    payload:endList
                });
                yield put({
                    type:'updateAll',
                    payload:allCount
                })
            }else{
                message.error(data.msg)
            }
        },
        //普通用户添加报修
        *addRepair({payload},{call,put}){
            const res = yield call(addRepair,payload);
            if(res.code === '200'){
                yield put({
                    type:'queryNotreat',
                    payload:{
                        size:10,
                        start: 1
                    }
                })
                message.success('添加成功！',3);
            }else{
                message.error(res.msg);
            }
        },
        //操作员同意报修
        *allowRepair({payload},{call,put}){
            const res = yield call(allowRepair,payload);
            if(res.code === '200'){
                message.success('已同意报修！')
                yield put({
                    type:'queryNotreat',
                    payload:{
                        size:10,
                        start:1
                    }
                })
            }else{
                message.error(res.msg);
            }
        },
        //操作员拒绝报修
        *rejectRepair({payload},{call,put}){
            const res = yield call(rejectRepair,payload);
            if(res.code === '200'){
                yield put({
                    type:'queryNotreat',
                    payload:{
                        size:10,
                        start:1
                    }
                })
                message.success('已经拒绝该报修！');
            }else{
                message.error(res.msg)
            }
        },
        //操作员输入结果
        *commentRepair({payload},{call,put}){
            const res = yield call(commentRepair,payload);
            if(res.code === '200'){
                yield put({
                    type:'queryNotreat',
                    payload:{
                        size:10,
                        start:1
                    }
                });
                yield put(routerRedux.push({
                    pathname:'/chzu/ingList'
                }))
                message.success('添加结果成功！')
            }else{
                message.error(res.msg)
            }
        }


    },

    reducers: {
        updateState(state,{payload}){
            return{
                ...state,
                notreatList:payload
            }
        },
        updateAll(state,{payload}){
            return{
                ...state,
                allCount:payload
            }
        },
        updateIng(state,{payload}){
            return{
                ...state,
                ingList:payload
            }
        },
        updateEnd(state,{payload}){
            return {
                ...state,
                endList:payload
            }
        },
        updateDetail(state,{payload}){
            return {
                ...state,
                detailList:payload
            }
        }
    },
})
